import { request } from 'umi';

/**
* get one record by id
*/
export async function get@{modelName}ById(id: number, params: object = {}) {
    return request<ApiResponse.ResultModel<@{modelName}Type.@{modelName}Response>>(`/api/@{apiRoute}/getById/${id}`, {
        method: 'GET',
        params: params
    });
}

/**
* get all records
*/
export async function get@{modelName}All(params: object = {}) {
    return request<ApiResponse.ResultModel<@{modelName}Type.@{modelName}Response>>(`/api/@{apiRoute}/getAll`, {
        method: 'GET',
        params: params
    });
}

/*
* get records by page
*/
export async function get@{modelName}PageList(params: object = {}) {
    return request<PageInfo.PageResponse<@{modelName}Type.@{modelName}Response>>(`/api/@{apiRoute}/getAll`, {
        method: 'GET',
        params: params
    });
}

/*
* update partial field of record
*/
export async function commonUpdate(id: number, fieldName: string, value: string) {
    return request<ApiResponse.ResultModel<@{modelName}Type.@{modelName}>>(`/api/@{apiRoute}/PatchById?id=${id}`, {
        method: 'PATCH',
        headers: {
            'Content-Type': 'application/json',
        },
        data: [{
            "id": id,
            "op": "replace",
            "path": `/${fieldName}`,
            "value": value

        }]
    });
}

/*
* update record
*/
export async function update@{modelName}(params: @{modelName}Type.@{modelName}Response) {
    return request<ApiResponse.ResultModel<boolean>>(`/api/@{apiRoute}/put`, {
        method: 'put',
        headers: {
            'Content-Type': 'application/json',
        },
        data: [params],
    });
}

/*
* add one record
*/
export async function add@{modelName}(params: @{modelName}Type.@{modelName}Response) {
    return request<ApiResponse.ResultModel<@{modelName}Type.@{modelName}Response>>('/api/@{apiRoute}/add', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: [params],
    });
}

export async function delete@{modelName}(params: DeleteRequest) {
    return request<ApiResponse.ResultModel<boolean>>(`/api/@{apiRoute}/delete`, {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
        },
        data: [params.id],
    });
}
